package com.baosight.risk.common.utils;


import com.baosight.risk.service.fltd.vo.BSWMessage;

import java.util.*;

/**
 * @author lishaoqing
 *
 */
public class BSWMessageUtils {
	
	public static Map read(String propertiesFile) {
		ResourceBundle rb = ResourceBundle.getBundle(propertiesFile);
		Map map = new HashMap();
		Enumeration enu = rb.getKeys();
		while (enu.hasMoreElements()) {
			Object obj = enu.nextElement();
			Object objv = rb.getObject(obj.toString());
			map.put(obj, objv);
		}
		return map;
	}
	
	/**
	 * 将domino转成发送智慧工作平台的消息体
	 * @param bean
	 * @return
	 */

    public static String getSendMsg(BSWMessage bean){
    	 java.text.DateFormat format= new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	String msgInfo="";
    	Map<Object, Object> mapUtil=read("BSWMessage");
    	String preStr=(String) mapUtil.get("messagePre");
    	preStr=preStr.replace("UUIDS", bean.getTo()+ DateUtils.format(new Date(), "yyyy-MM-ddHH:mm:ss"));
    	String stuffStr=(String) mapUtil.get("messageStuff");
    	if(bean==null){
    		return null;
    	}
    	if(bean.getMessageType()==null){
    		return null;
    	}
    	if(bean.getSendSysXbusCode()==null){
    		return null;
    	}
    	if(bean.getIdentification()==null && bean.getMsgNo()==null){
    		return null;
    	}
    	if(bean.getSubject()==null){
    		return null;
    	}
    	if(bean.getTo()==null){
    		return null;
    	}
    	if(bean.getExpiryDate()==null){
    		return null;
    	}
    	//preStr=preStr.replaceAll("$$$$", bean.getSendSysXbusCode());
    	String tmpStr="";
    	if(bean.getProtocolVersion()!=null){
    		tmpStr="\""+bean.getProtocolVersion()+"\",";
    	}else{
    		tmpStr=  "\"1.0\",";
    	}
    	preStr=preStr+tmpStr;
    	tmpStr="\""+bean.getMessageType()+"\",";
    	preStr=preStr+tmpStr;
    	if(bean.getMessageSource()!=null){
    		tmpStr="\""+bean.getMessageSource()+"\",";
    	}else{
    		tmpStr=  "\"\",";
    	}
    	preStr=preStr+tmpStr;
    	
    	if(bean.getIdentification()!=null){
    		tmpStr="\""+bean.getIdentification()+"\",";
    	}else{
    		tmpStr=  "\"\",";
    	}
    	preStr=preStr+tmpStr;
    	tmpStr=  "\"\",";
    	preStr=preStr+tmpStr;
    	
    		tmpStr="\""+bean.getSubject()+"\",";
    	
    	preStr=preStr+tmpStr;
    	if(bean.getUrl()!=null){
    		tmpStr="\""+bean.getUrl()+"\",";
    	}else{
    		tmpStr=  "\"#\",";
    	}
    	preStr=preStr+tmpStr;
    	if(bean.getFrom()!=null){
    		tmpStr="\""+bean.getFrom()+"\",";
    	}else{
    		tmpStr=  "\"#\",";
    	}
    	preStr=preStr+tmpStr;
    	tmpStr="\""+bean.getTo()+"\",";
    	preStr=preStr+tmpStr;
    	if(bean.getBody()!=null){
    		tmpStr="\""+bean.getBody()+"\",";
    	}else{
    		tmpStr=  "\"\",";
    	}
    	preStr=preStr+tmpStr;
    	if(bean.getMsgNo()!=null){
    		tmpStr="\""+bean.getMsgNo()+"\",";
    	}else{
    		tmpStr=  "\"\",";
    	}
    	preStr=preStr+tmpStr;
    	tmpStr="\""+format.format(new Date())+"\",";
    	preStr=preStr+tmpStr;
    		tmpStr="\""+format.format(bean.getExpiryDate())+"\"";
    	preStr=preStr+tmpStr;
    	msgInfo=preStr+stuffStr;
    	return msgInfo;
    }

    public static String generateMsgNo(){
		return "RISK"+ DateUtils.formatYYYYMMDD(new Date())+IDGenerator.getIdStr();
	}

	public static String generateIdentification(String userCode){
    	return userCode + DateUtils.format(new Date(), "yyyy-MM-ddHH:mm:ss");
	}

}
